package com.cbts.service.person.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.cbts.dao.ExchangeRecordDao;
import com.cbts.dao.GiftDao;
import com.cbts.dao.RuleDao;
import com.cbts.dao.UserInfoDao;
import com.cbts.entity.ExchangeRecord;
import com.cbts.entity.ExchangeRecordId;
import com.cbts.entity.Gift;
import com.cbts.entity.Rule;
import com.cbts.entity.UserInfo;
import com.cbts.service.person.ExchangeService;

public class ExchangeServiceImpl implements ExchangeService{

	private ExchangeRecordDao exchangeRecordDao;//与点数兑换记录有关的数据访问
	private RuleDao ruleDao;//与点数兑换规则有关的数据访问
	private GiftDao giftDao;//与兑换物品有关的数据访问
	private UserInfoDao userInfoDao;//与员工管理有关的数据访问
	public GiftDao getGiftDao() {
		return giftDao;
	}

	public void setGiftDao(GiftDao giftDao) {
		this.giftDao = giftDao;
	}

	public ExchangeRecordDao getExchangeRecordDao() {
		return exchangeRecordDao;
	}

	public void setExchangeRecordDao(ExchangeRecordDao exchangeRecordDao) {
		this.exchangeRecordDao = exchangeRecordDao;
	}

	/*public boolean addExchangeRecord(ExchangeRecord exchangeRecord) {
		
		if(exchangeRecordDao.save(exchangeRecord))
			return true;
		return false;
	}*/

	public RuleDao getRuleDao() {
		return ruleDao;
	}

	public void setRuleDao(RuleDao ruleDao) {
		this.ruleDao = ruleDao;
	}

	public UserInfoDao getUserInfoDao() {
		return userInfoDao;
	}

	public void setUserInfoDao(UserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	//进行兑换
	public String exchange(Gift gift, int count,UserInfo userInfo) {
		
		Rule rule = ruleDao.get(1);
		if(gift.getNumber() < count)//物品的库存量小于要兑换的数量
			return "数量不足，兑换失败";
		if(gift.getGiftValue()*count*rule.getRate() >  userInfo.getCount())//兑换所需要的点数大于员工所拥有的点数
			return "您的点数兑换不足，兑换失败";
		ExchangeRecordId exchangeRecordId = new ExchangeRecordId(new Timestamp(new Date().getTime()),userInfo.getUserid());
		ExchangeRecord exchangeRecord = new ExchangeRecord(exchangeRecordId,
				gift,userInfo,gift.getGiftValue()*count*rule.getRate(),"否",count);//生成点数兑换记录
		exchangeRecordDao.save(exchangeRecord);//增加点数兑换记录
		if(!"现金".equals(gift.getGiftName()))
		{
			gift.setNumber(gift.getNumber()-count);//设置物品的点数
			giftDao.update(gift);//修改物品的点数
		}
		//userInfo.setCount(userInfo.getCount()-gift.getGiftValue()*count*rule.getRate());
		//userInfoDao.update(userInfo);
		return "兑换成功";
	}

	public List<ExchangeRecord> getAllExchangeRecord() {
		
		return exchangeRecordDao.findAll().getPageItems();
	}

	public List<ExchangeRecord> getExchangeRecordByUserid(String userid) {

		
		return exchangeRecordDao.getExchangeRecordByUserid(userid);
	}

	public ExchangeRecord getExchangeRecord(ExchangeRecordId exchangeRecordId) {
		// TODO Auto-generated method stub
		return exchangeRecordDao.get(exchangeRecordId);
		/*String hql = "and o.id.exchangeTime = ? and o.id.userid = ?";
		return exchangeRecordDao.queryUnique(hql, new Object[]{exchangeRecordId.getExchangeTime(),exchangeRecordId.getUserid()});*/
	}

	public boolean updateExchangeRecord(ExchangeRecord exchangeRecord) {
		
		return exchangeRecordDao.update(exchangeRecord);
	}

/*	public boolean exchangeMoney(double money,UserInfo userInfo) {
		
		Rule rule = ruleDao.get(1);
		if(money*rule.getRate()>userInfo.getCount())
			return false;
		userInfo.setCount(userInfo.getCount()-money*rule.getRate());
		userInfoDao.update(userInfo);
		return false;
	}*/

	

}
